<?php

class Home extends Controller {
	
	function index() {
		$this->set('title','GITBOX --Welcome to gitbox !');
		//$photo=$this->HomeModel->GetAllPhotoByViewSetting(1,2,0,0);
		//$this->set('result',$photo);
	}
	function register(){
		$field = array();
		$field['name'] = $_POST['namereg'];
		$field['username'] = $_POST['usernamereg'];
		$field['password'] = $_POST['passwordreg'];
		$field['email'] = $_POST['emailreg'];
		$permitted = $this->HomeModel->checkusername($field['username']);
		if(count($permitted)==1){
			// Not permitted
			$this->set('result',1);
		}else{
			// Permitted
			$result = $this->HomeModel->insertUserProfile($field);
			$this->set('result',$result);
		}
	}
	
	function login(){
		$field = array();
		$field['username'] = $_POST['username'];
		$field['password'] = $_POST['password'];
		$result = $this->HomeModel->validateUserandPassword($field);
		//$this->_auto_render = false;
		if(count($result) != 0){
			//success login
			$this->session->set('Username',$result[0]['User']['Username']);
			$this->session->set('Workdir', 'workdirserver/'.$result[0]['User']['Username']);
			$this->redirect(BASE_PATH.'/panel/index');
		}else{
			//failed login
			$this->set('title','Failed');
			$this->redirect(BASE_PATH.'/home/index');
		}
	}
}
